package com.humanminoritygame.web;

import java.util.SortedSet;

import javax.servlet.http.HttpSession;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.humanminoritygame.model.CategoryRepo;
import com.humanminoritygame.model.Question;
import com.humanminoritygame.model.QuestionRepo;
import com.humanminoritygame.model.Room;
import com.humanminoritygame.model.RoomRepo;
import com.humanminoritygame.model.Round;
import com.humanminoritygame.model.SubscriptionRepo;
import com.humanminoritygame.model.User;
import com.humanminoritygame.model.UserRepo;
import com.humanminoritygame.model.UserRoundResultRepo;
import com.humanminoritygame.model.UserType;

@Controller
public class AdminController {

  SubscriptionRepo subsciptions;
  QuestionRepo questions;
  CategoryRepo categories;
  UserRepo userRepo;
  RoomRepo roomRepo;
  UserRoundResultRepo userRoundResults;
  SessionFactory sessionFactory;

  @Autowired
  public AdminController(SubscriptionRepo subscriptions, QuestionRepo questions, CategoryRepo categories, UserRepo users, RoomRepo rooms, UserRoundResultRepo userRoundResults, SessionFactory sessionFactory){
    this.userRepo = users;
    this.subsciptions = subscriptions;
    this.questions = questions;
    this.categories = categories;
    this.roomRepo = rooms;
    this.userRoundResults = userRoundResults;
    this.sessionFactory = sessionFactory;
  }
  
  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView listAllUsers(HttpSession session){
    ModelAndView mav = new ModelAndView();
    mav.setViewName("adminListAllUsers");
    User user = (User)session.getAttribute("user");
    if(!user.isAdmin()){
      throw new RuntimeException("Only administrators can access this. If you keep trying this, we will ban your account!");
    }
    SortedSet<User> users = userRepo.getHumanUsers(); 
    mav.addObject("users", users);
    return mav;
  }
  
  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView listAllClosedRooms(HttpSession session){
    ModelAndView mav = new ModelAndView();
    mav.setViewName("adminListAllClosedRooms");
    User user = (User)session.getAttribute("user");
    if(!user.isAdminOrResearcher()){
      throw new RuntimeException("Only administrators can access this. If you keep trying this, we will ban your account!");
    }
    SortedSet<Room> rooms = roomRepo.getClosedRooms();
    mav.addObject("rooms", rooms);
    return mav;
  }
  
  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView viewClosedRoom(HttpSession session, @RequestParam("id") Long roomId){
    ModelAndView mav = new ModelAndView();
    mav.setViewName("adminViewClosedRoom");
    User user = (User)session.getAttribute("user");
    if(!user.isAdminOrResearcher()){
      throw new RuntimeException("Only administrators can access this. If you keep trying this, we will ban your account!");
    }
    Room room = roomRepo.getRoomById(roomId);
    mav.addObject("room", room);
    mav.addObject("subscriptions", subsciptions.getRoomSubscriptionsOrderByScore(room));
    mav.addObject("question", new Question("A or B?", "A", "B"));
    String summaries = new String("[0, 0], ");
    int i=1;
    for(Round r : room.getRounds()){
      if(r.hasElapsed() && r.getSummary() != null){
        summaries += "[" + i + ", " + r.getSummary().getAnswerValue() + "], ";
        i++;
      }
    }
    if(summaries.length()>0){
      //le saca la coma y el espacio del final
      summaries = summaries.substring(0, summaries.length()-2);
    }
    mav.addObject("summaries", summaries);
    return mav;
  }
  
  
  @RequestMapping(method = RequestMethod.GET)
  public String makeAdmin(HttpSession session, @RequestParam("id") Long userId){
    return setType(session, userId, UserType.ADMIN);
  }
  
  @RequestMapping(method = RequestMethod.GET)
  public String makeResearcher(HttpSession session, @RequestParam("id") Long userId){
    return setType(session, userId, UserType.RESEARCHER);
  }
  
  @RequestMapping(method = RequestMethod.GET)
  public String makePlayer(HttpSession session, @RequestParam("id") Long userId){
    return setType(session, userId, UserType.PLAYER);
  }
  
  private String setType(HttpSession session, Long userId, UserType type){
    User currentUser = (User)session.getAttribute("user");
    if(!currentUser.isAdmin()){
      throw new RuntimeException("Only administrators can access this. If you keep trying this, we will ban your account!");
    }
    User user = userRepo.getUserById(userId);
    if(user!=null){
      user.setType(type);
    }
    return "redirect:listAllUsers";
  }
}
